package com.hh.controller;

import com.hh.common.BaseResponse;
import com.hh.common.ErrorCode;
import com.hh.common.ResultUtils;
import com.hh.entity.domain.Blog;
import com.hh.entity.domain.Message;
import com.hh.entity.domain.User;
import com.hh.entity.dto.thumb.DoThumbRequest;
import com.hh.entity.enums.BehaviorType;
import com.hh.entity.enums.MessageType;
import com.hh.exception.BusinessException;
import com.hh.exception.ThrowUtils;
import com.hh.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * com.hh.controller
 *
 * @author hanhui
 * @version 0.0.1
 * @date 2025/6/1
 **/
@RestController
public class ThumbController {

    @Resource
    private ThumbService thumbService;
    @Resource
    private UserService userService;
    @Resource
    private MessageService messageService;
    @Resource
    private BlogService blogService;
    @Resource
    private UserBehaviorService userBehaviorService;

    /**
     * 处理用户点赞请求
     *
     * @param doThumbRequest 点赞请求参数，包含被点赞的内容ID等信息
     * @param request        HTTP请求对象，用于获取用户会话信息
     * @return BaseResponse<Boolean> 操作结果，true表示点赞成功，false表示失败
     */
    @PostMapping("/like")
    public BaseResponse<Boolean> like(@RequestBody DoThumbRequest doThumbRequest, HttpServletRequest request) {
        Long postId = doThumbRequest.getPostId();
        ThrowUtils.throwIf(postId == null, ErrorCode.PARAMS_ERROR);

        boolean success = thumbService.doThumb(doThumbRequest, request);
        ThrowUtils.throwIf(Boolean.FALSE.equals(success), ErrorCode.OPERATION_ERROR);

        Long loginUserId = userService.getLoginUser(request).getId();
        User user = userService.getById(loginUserId);
        // 添加行为
        userBehaviorService.addBehavior(user.getId(), postId, BehaviorType.LIKE.getValue());
        // 发送点赞通知
        Long targetId = null;
        Blog blog = blogService.getById(postId);
        if (blog != null) {
            targetId = blog.getUserId();
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文章不存在");
        }
        Message message = Message.builder()
                .userId(targetId)
                .content(String.format("%s点赞了你的博客", user.getUserName()))
                .type(MessageType.NOTICE.getValue())
                .relatedId(postId).build();

        messageService.sendMessageToUser(message);
        return ResultUtils.success(success);
    }

    /**
     * 处理用户取消点赞请求
     *
     * @param doThumbRequest 取消点赞请求参数，包含被取消点赞的内容ID等信息
     * @param request        HTTP请求对象，用于获取用户会话信息
     * @return BaseResponse<Boolean> 操作结果，true表示取消点赞成功，false表示失败
     */
    @PostMapping("/unlike")
    public BaseResponse<Boolean> unlike(@RequestBody DoThumbRequest doThumbRequest, HttpServletRequest request) {
        return ResultUtils.success(thumbService.unThumb(doThumbRequest, request));
    }

    /**
     * 查询用户是否已点赞指定内容
     *
     * @param postId  内容ID，用于查询指定内容的点赞状态
     * @param request HTTP请求对象，用于获取当前登录用户信息
     * @return BaseResponse<Boolean> 查询结果，true表示已点赞，false表示未点赞
     */
    @GetMapping("/hasLiked/{postId}")
    public BaseResponse<Boolean> getLikeStatus(@PathVariable Long postId,
                                               HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        boolean hasLiked = thumbService.hasThumb(userId, postId);
        return ResultUtils.success(hasLiked);
    }
}